package com.starry.home.logic.network

import androidx.lifecycle.liveData
import com.starry.home.logic.model.hospital.RegisterBean
import com.starry.home.logic.model.login.LoginBean
import com.starry.home.logic.model.login.UserInfoBean
import com.starry.home.logic.model.medical.MedicalBean
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

object Repository {

    /**
     * 登录
     */
    fun login(params: LoginBean) = liveData(Dispatchers.IO) {
        val result = try {
            val response = AppNetWork.login(params)
            if (response?.msg == "success") {
                Result.success(response)
            } else {
                Result.failure(RuntimeException("response status is ${response?.msg}"))
            }
        } catch (e: Exception) {
            Result.failure(e)
        }
        emit(result)
    }

    /**
     * 注册
     */
    fun register(params: LoginBean) = liveData(Dispatchers.IO) {
        val result = try {
            val response = AppNetWork.register(params)
            if (response?.msg == "success") {
                Result.success(response)
            } else {
                Result.failure(RuntimeException("${response?.msg}"))
            }
        } catch (e: Exception) {
            Result.failure(e)
        }
        emit(result)
    }

    /**
     * 患者信息
     */
    fun updateUserInfo(userInfo: UserInfoBean) = liveData(Dispatchers.IO) {
        val result = try {
            val response = AppNetWork.updateUserInfo(userInfo)
            if (response.msg == "success") {
                Result.success(response)
            } else {
                Result.failure(RuntimeException(response.msg))
            }
        } catch (e: Exception) {
            Result.failure(e)
        }
        emit(result)
    }

    fun getUserInfo(userId: Int) = liveData(Dispatchers.IO) {
        val result = try {
            val response = AppNetWork.getUserInfo(userId)
            if (response.msg == "success") {
                Result.success(response)
            } else {
                Result.failure(RuntimeException("response status is ${response.msg}"))
            }
        } catch (e: Exception) {
            Result.failure(e)
        }
        emit(result)
    }


    fun getGhListByHzIdAndWeek(hzId: Int) = liveData(Dispatchers.IO) {
        val result = try {
            val response = AppNetWork.getGhListByHzIdAndWeek(hzId)
            if (response.msg == "success") {
                Result.success(response)
            } else {
                Result.failure(RuntimeException("response status is ${response.msg}"))
            }
        } catch (e: Exception) {
            Result.failure(e)
        }
        emit(result)
    }

    fun getSectionList(params: Int) = liveData(Dispatchers.IO) {
        val result = try {
            val response = AppNetWork.getSectionList(params)
            if (response.msg == "success") {
                Result.success(response)
            } else {
                Result.failure(RuntimeException("response status is ${response.msg}"))
            }
        } catch (e: Exception) {
            Result.failure(e)
        }
        emit(result)
    }

    fun getDoctorList(ysSection: String) = liveData(Dispatchers.IO) {
        val result = try {
            val response = AppNetWork.getDoctorList(ysSection)
            if (response.msg == "success") {
                Result.success(response)
            } else {
                Result.failure(RuntimeException("response status is ${response.msg}"))
            }
        } catch (e: Exception) {
            Result.failure(e)
        }
        emit(result)
    }

    suspend fun getYsInfo(ysId: Int) = withContext(Dispatchers.IO) {
        AppNetWork.getYsInfo(ysId)
    }

    suspend fun saveRegister(registerBean: RegisterBean) = withContext(Dispatchers.IO) {
        AppNetWork.saveRegister(registerBean)
    }

    suspend fun getRegisterList(hzId: Int) = withContext(Dispatchers.IO) {
        AppNetWork.getRegisterList(hzId)
    }


    fun queryYsByName(ysName: String) = liveData(Dispatchers.IO) {
        val result = try {
            val params = HashMap<String, String>()
            //?key={ysName}&paramType=EMR_ys_name&page=1&limit=10
            params["key"] = ysName
            params["paramType"] = "EMR_ys_name"
            params["page"] = "1"
            params["limit"] = "10"
            val response = AppNetWork.queryYsByName(params)
            if (response.msg == "success") {
                Result.success(response)
            } else {
                Result.failure(RuntimeException("response status is ${response.msg}"))
            }
        } catch (e: Exception) {
            Result.failure(e)
        }
        emit(result)
    }


    fun getXyJdByBlId(blId: Int) = liveData(Dispatchers.IO) {
        val result = try {
            val response = AppNetWork.getXyJdByBlId(blId)
            if (response.msg == "success") {
                Result.success(response)
            } else {
                Result.failure(RuntimeException("response status is ${response.msg}"))
            }
        } catch (e: Exception) {
            Result.failure(e)
        }
        emit(result)
    }

    fun getZyJdByBlId(blId: Int) = liveData(Dispatchers.IO) {
        val result = try {
            val response = AppNetWork.getZyJdByBlId(blId)
            if (response.msg == "success") {
                Result.success(response)
            } else {
                Result.failure(RuntimeException("response status is ${response.msg}"))
            }
        } catch (e: Exception) {
            Result.failure(e)
        }
        emit(result)
    }

    fun getBlIdByHzId(hzId: Int) = liveData(Dispatchers.IO) {
        val result = try {
            val response = AppNetWork.getBlIdByHzId(hzId)
            if (response.msg == "success") {
                Result.success(response)
            } else {
                Result.failure(RuntimeException("response status is ${response.msg}"))
            }
        } catch (e: Exception) {
            Result.failure(e)
        }
        emit(result)
    }

    fun saveBook(medical: MedicalBean.BlEntity) = liveData(Dispatchers.IO) {
        val result = try {
            val response = AppNetWork.saveBook(medical)
            if (response.msg == "success") {
                Result.success(response)
            } else {
                Result.failure(RuntimeException("response status is ${response.msg}"))
            }
        } catch (e: Exception) {
            Result.failure(e)
        }
        emit(result)
    }

}